package com.example.farmeri.common;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Create by weiqishan on 2020/5/28.
 */
public abstract class BaseRecycleOnScrollListener extends RecyclerView.OnScrollListener {

    //是否向上滑动
    private boolean isSlidingUpward = false;

    @Override
    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        //不滑动时
        if (newState == RecyclerView.SCROLL_STATE_IDLE){
            int lastItemPosition = layoutManager.findLastVisibleItemPosition();
            int itemCount = layoutManager.getItemCount();

            if (lastItemPosition == (itemCount - 1) && isSlidingUpward){
                //加载更多
                onLoadMore();
            }
        }
    }

    @Override
    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        Log.d("TAG", "onScrolled: dy = "+ dy);
        Log.d("TAG", "onScrolled: dx = "+ dx);
        isSlidingUpward = ( dy > 0 );
    }
    public abstract void onLoadMore();
}
